<?php
/* MyMeeting is an online meeting management software.
 * Copyright (C) 2009 Government Of Malaysia
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * @author: Abdullah Zainul Abidin, Nuhaa All Bakry
 *          Eavay Javay Barnad, Sarogini Muniyandi, Teow Jit Huan
 *
 */

class TemplatesController extends AppController {

    /**
     * Define $name
     *
     */
    var $name = 'Templates';
    /**
     * Define $helpers
     *
     */
    var $helpers = array('Html', 'Form','Text','Javascript');
    /**
     * Define $uses
     *
     */
    var $uses = array('Template','Committee');

    /**
     * Describe index
     *
     * @return null
     */
    function index() {
        $this->Committee->unbindModel(array('hasMany'=>array('Announcement','Item','Group','Meeting','Decision','Todo','Userstatus','Groupstatus','Workflow','Wfmodel'),'hasAndBelongsToMany'=>array('User','Membership')),false);
        $this->Template->recursive = 0;
        //$this->Template->Behaviors->detach('Workflow');
        // first page
        if (empty($this->params['named']['page']) || empty($this->data)) {
            $this->Session->delete('Search.title'); 
            $this->Session->delete('Search.description'); 
            $this->Session->delete('Search.message'); 
        }

        // get the search terms
        if(!empty($this->data['Search']['title'])) $cursearch_title = $this->data['Search']['title'];
        elseif($this->Session->check('Search.title')) $cursearch_title = $this->Session->read('Search.title'); 
        else $cursearch_title = '';
        if(!empty($this->data['Search']['description'])) $cursearch_description = $this->data['Search']['description'];
        elseif($this->Session->check('Search.description')) $cursearch_description = $this->Session->read('Search.description'); 
        else $cursearch_description = '';
        if(!empty($this->data['Search']['message'])) $cursearch_message = $this->data['Search']['message'];
        elseif($this->Session->check('Search.message')) $cursearch_message = $this->Session->read('Search.message'); 
        else $cursearch_message = '';

        // construct queries
        $filters = array();
        if(isset($cursearch_title) && $cursearch_title!='') {
            array_push($filters,"Template.title  like '%".$cursearch_title."%'");
            $this->Session->write('Search.title', $cursearch_title);        
        }
        if(isset($cursearch_description) && $cursearch_description!='') {
            array_push($filters,"Template.description  like '%".$cursearch_description."%'");
            $this->Session->write('Search.description', $cursearch_description);        
        }
        if(isset($cursearch_message) && $cursearch_message!='') {
            array_push($filters,"Template.template  like '%".$cursearch_message."%'");
            $this->Session->write('Search.message', $cursearch_message);        
        }

        if(isset($this->params['committee'])){
            $dcommittee=$this->Committee->findByShortName($this->params['committee']);
            $this->set('dcommittee',$dcommittee);
            $rules=array_merge($filters,array('model'=>'Committee','foreign_key'=>$dcommittee['Committee']['id']));
        }
        else{
            //$this->layout='mainpage';
            $rules=array_merge($filters,array('model'=>array('System','SystemOnly')));
        }
        $templates=$this->paginate('Template',$rules);
        $this->set('dcommittee', $this->dcommittee);
        $this->set('templates', $templates);
    }

    /**
     * Describe view
     *
     * @param $id
     * @return null
     */
    function view($id = null) {
        $this->Committee->unbindModel(array('hasMany'=>array('Announcement','Item','Group','Meeting','Decision','Todo','Userstatus','Groupstatus','Workflow','Wfmodel'),'hasAndBelongsToMany'=>array('User','Membership')),false);
        if(isset($this->params['committee'])){
            $this->set('dcommittee',$this->dcommittee);
            $id=$this->params['id'];
        }
        if (!$id) {
            $id=$this->params['named']['id'];
        }
        if (!$id) {
            $this->Session->setFlash(__('Invalid Template.', true));
            $this->redirect(array('action'=>'index'));
        }
        $this->set('template', $this->Template->read(null, $id));
    }

    /**
     * Describe add
     *
     * @return null
     */
    function add() {
        //$this->layout='mainpage';
        if (!empty($this->data)) {
            $this->Template->create();
            if ($this->Template->save($this->data)) {
                $this->Session->setFlash(__('The Template has been saved', true));
                $this->redirect(array('action'=>'index'));
            } else {
                $this->Session->setFlash(__('The Template could not be saved. Please, try again.', true));
            }
        }
    }

    /**
     * Describe edit
     *
     * @param $id
     * @return null
     */
    function edit($id = null) {
        $this->Committee->unbindModel(array('hasMany'=>array('Announcement','Item','Group','Meeting','Decision','Todo','Userstatus','Groupstatus','Workflow','Wfmodel'),'hasAndBelongsToMany'=>array('User','Membership')),false);
        if(isset($this->params['committee'])){
            $dcommittee=$this->Committee->findByShortName($this->params['committee']);
            $this->set('dcommittee',$dcommittee);
            $id=$this->params['id'];
        }
        else{
            //$this->layout='mainpage';
        }
        if (!$id) {
            $id=$this->params['named']['id'];
        }
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid Template', true));
            $this->redirect(array('action'=>'index'));
        }
        if (!empty($this->data)) {
            if ($this->Template->save($this->data)) {
                $this->Session->setFlash(__('The Template has been saved', true));
                if(isset($this->data['Template']['returnpage'])) {
                    $this->redirect($this->data['Template']['returnpage']);
                }
                elseif(isset($dcommittee)){
                    $this->redirect(array('committee'=>$dcommittee['Committee']['short_name'],'action'=>'index'));
                }
                else{
                    $this->redirect(array('action'=>'index'));
                }
            } else {
                $this->Session->setFlash(__('The Template could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->Template->read(null, $id);
            $this->set('template', $this->data);
        }
    }

    /**
     * Describe delete
     *
     * @param $id
     * @return null
     */
    function delete($id = null) {
        $this->layout='mainpage';
        if (!$id) {
            $this->Session->setFlash(__('Invalid id for Template', true));
            $this->redirect(array('action'=>'index'));
        }
        $this->Template->del($id);
        $this->Session->setFlash(__('Template deleted', true));
        $this->redirect(array('action'=>'index'));
    }

    function guide() {
        $this->layout = 'popup';
    }
    
    function reset() {
        $sys_tmpl = $this->Template->findAllByModel('System');
        foreach ($sys_tmpl as $sys) {
            $comm_tmpl = $this->Template->find(array('foreign_key'=>$this->dcommittee['Committee']['id'],'model'=>'Committee','type'=>$sys['Template']['type']));
            $comm_tmpl['Template']['template'] = $sys['Template']['template'];
            $comm_tmpl['Template']['description'] = $sys['Template']['description'];
            $comm_tmpl['Template']['title'] = $sys['Template']['title'];
            $this->Template->save($comm_tmpl);
        }
        $this->Session->setFlash(__('The messages have been reset', true));
        if(isset($this->data['Template']['returnpage'])) {
            $this->redirect($this->data['Template']['returnpage']);
        }
        elseif(isset($this->dcommittee)){
            $this->redirect(array('committee'=>$this->dcommittee['Committee']['short_name'],'controller'=>'Templates','action'=>'index'));
        }
        else{
            $this->redirect(array('action'=>'index'));
        }
    }
    
    function config(){
        if(!empty($this->data)){
            $delete=array();
            $add=array();
            $output="<?php\n";
            foreach($this->data['Template'] as $set=>$setting){
                $output.="\tConfigure::write('$set','$setting');\n";
            }
            $output.="?>\n";
            $dfile= APP . 'config' . DS . 'email.php';
        
            $file = new File($dfile);
            if ($file->exists())  $file->delete(); 
            $file->create();
        
            if (!$file->writable()) return false;
            else {
                $file->open('w');
                $file->write($output);    
                $file->close();
                return true;
            }
        }
    }
}
?>
